package com.yyh.BookLendSystem.dao;

import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import API.JDBC.Ten26.DBHelper;

public class BookDao {
	
	public List<Map<String,Object>> select (String name,String author ,String prices){
		//组合条件查询
		String sql = "select * from book where 1=1 ";
		ArrayList<Object> parms =new ArrayList<>();
		if(name!=null && name.isEmpty()==false) {
			sql+=" and name like '%'|| ? ||'%'";
			parms.add(name);
		}
		if(author !=null && author.isEmpty()==false) {
			sql+=" and author like '%'|| ? ||'%'";
			parms.add(author);
		}
		if(prices!=null && prices.isEmpty()==false ) {
			sql= " and price between ? and ?";
			String pricesStr[] = prices.split("~");
			parms.add(pricesStr[0]);
			parms.add(pricesStr[1]);
		}	
		sql+=" order by id";
		try {
			return DBHelper.selectListMap(sql, parms.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Map<String,Object>>();
		}
	}
	
	
	
	public void insert(String id,String name,String author,Date pressdate,int price) throws SQLException {
		String sql = "insert into book values(seq_pub.nextval,?,?,null,?,default,?)";
		DBHelper.update(sql,name,author,pressdate,price);
	}
	
	public void update(String id,String name,String author,Date pressdate,int price) throws SQLException {
		String sql = "update book set name = ?, author = ?, pressdate = ?, price = ? where id = ?";
		DBHelper.update(sql, name,author,pressdate,price,id);
	}
	public void selectNums() {
	}

}
